﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Infrastructure.Account;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Infrastructure.Facilities;
using Gfw.Models.Account;
using Gfw.Models.Stock;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.Infrastructure.Stock.Facilities
{
    /// <summary>
    /// 股票卖出的个人帐户利润分配，包括70到现金币，30到股票币
    /// </summary>
    public class StockProfit : AwardAllot
    {
        private StkUseraccount _stkUserAccount;

        public StockProfit(StkUseraccount stkUserAccount, decimal award, string awardSerialNo, string taxationSerialNo)
            :base(stkUserAccount.UserId,award,awardSerialNo,taxationSerialNo,UserTradeTypeEnum.Sell_Stock)
        {
            _stkUserAccount = stkUserAccount;
        }

        public virtual void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            if (unitOfWork == null)
                throw new GfwTransactionInvalidException(ConstMessage.TransactionInvalid);

            UnitOfWork work = unitOfWork;

            ActUserAccountinformationRepository userAccountRepository = new ActUserAccountinformationRepository();

            ActUserAccountinformation userAmount = userAccountRepository.Get(userId, work);
            //cash = Math.Floor(award*CashRatio);
            //emoney = Math.Floor(award*(1 - CashRatio));
            cash = Math.Round(award * CashRatio, 4);
            emoney = award - cash;
            decimal companyDec = award - cash - emoney;
            userAmount.Cash += cash;
            //userAmount.ElectronicMoney += emoney;

            userAccountRepository.Update(userAmount, work);

            if (cash > 0)
            {
                ActUserBargain4cashRepository cashRepository = new ActUserBargain4cashRepository();
                cashRepository.AddIncome(userId, (int)tradeType, cash, userAmount.Cash, awardSerialNo, work);
            }
            if (emoney > 0)
            {
                //在通用的动态奖金分配中，是分配到电子币的，只有股票卖出，才是分配30%到股票币
                _stkUserAccount.StockMoney += emoney;

            }
            if (companyDec > 0)
            {
                //ActCompanyBargainRepository companyRepository = new ActCompanyBargainRepository();
                //companyRepository.AddIncome(userId, (int)CompanyTradeTypeEnum.Decimal, companyDec, "",
                //                            awardSerialNo, null, work);
                throw new GfwTransactionInvalidException("已经取消零头转移了。");
            }


        }

    }
}
